package com.zzs.client.controller;

import com.zzs.client.pojo.query.ProblemQuery;
import com.zzs.client.pojo.vo.problem.ProblemDetailVO;
import com.zzs.client.pojo.vo.problem.ProblemVO;
import com.zzs.client.service.ProblemService;
import com.zzs.common.core.response.PageResult;
import com.zzs.common.core.response.Result;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zzs
 * @date 2025/03/25
 */
@RestController
@RequestMapping("/client/problems")
public class ProblemController {
    @Autowired
    private ProblemService problemService;

    @GetMapping("/{problemId}")
    public Result<ProblemDetailVO> getProblemDetail(@PathVariable("problemId") long problemId) {
        ProblemDetailVO problemDetail = problemService.getProblemDetail(problemId);
        return Result.success(problemDetail);
    }

    @GetMapping
    public Result<PageResult<ProblemVO>> listProblems(@Valid ProblemQuery problemQuery) {
        if (problemQuery.getKeyword() != null) {
            problemQuery.setKeyword(problemQuery.getKeyword().trim());
        }
        PageResult<ProblemVO> pageResult = problemService.listProblems(problemQuery);
        return Result.success(pageResult);
    }

    @GetMapping("/prev/{problemId}")
    public Result<String> getPrevProblemId(@PathVariable("problemId") long problemId) {
        long prevProblemId = problemService.getPrevProblemId(problemId);
        return Result.success(prevProblemId + "");
    }

    @GetMapping("/next/{problemId}")
    public Result<String> getNextProblemId(@PathVariable("problemId") long problemId) {
        long nextProblemId = problemService.getNextProblemId(problemId);
        return Result.success(nextProblemId + "");
    }
}
